package com.quizzer.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.quizzer.common.utils.DateUtils;
import com.quizzer.system.convert.AppUserConverter;
import com.quizzer.system.dto.user.appUser.AppUserAddDto;
import com.quizzer.system.dto.user.appUser.AppUserOutputDto;
import com.quizzer.system.dto.user.appUser.AppUserQueryDto;
import com.quizzer.system.dto.user.appUser.AppUserUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.quizzer.system.mapper.AppUserMapper;
import com.quizzer.system.domain.AppUser;
import com.quizzer.system.service.IAppUserService;

/**
 * 小程序用户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-07
 */
@Service
public class AppUserServiceImpl implements IAppUserService
{
    @Autowired
    private AppUserMapper appUserMapper;

    /**
     * 查询小程序用户
     *
     * @param id 小程序用户主键
     * @return 小程序用户
     */
    @Override
    public AppUserOutputDto selectAppUserById(Long id)
    {
        AppUser appUser = appUserMapper.selectAppUserById(id);
        return selectOne(appUser);
    }

    private AppUserOutputDto selectOne(AppUser appUser) {
        return AppUserConverter.toOutputDto(appUser);
    }

    /**
     * 查询小程序用户列表
     *
     * @param queryDto 小程序用户
     * @return 小程序用户
     */
    @Override
    public List<AppUserOutputDto> selectAppUserList(AppUserQueryDto queryDto)
    {
        AppUser appUser = AppUserConverter.toEntity(queryDto);
        List<AppUser> appUsers = appUserMapper.selectAppUserList(appUser);
        List<AppUserOutputDto> outputDtoList = new ArrayList<>();
        for (AppUser user : appUsers) {
            outputDtoList.add(selectOne(user));
        }
        return outputDtoList;
    }

    /**
     * 新增小程序用户
     *
     * @param addDto 小程序用户
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUserAddDto addDto)
    {
        AppUser appUser = AppUserConverter.toEntity(addDto);
        return appUserMapper.insertAppUser(appUser);
    }

    /**
     * 修改小程序用户
     *
     * @param appUser 小程序用户
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUserUpdateDto updateDto)
    {
        AppUser appUser = AppUserConverter.toEntity(updateDto);
        return appUserMapper.updateAppUser(appUser);
    }

    /**
     * 批量删除小程序用户
     *
     * @param ids 需要删除的小程序用户主键
     * @return 结果
     */
    @Override
    public int deleteAppUserByIds(Long[] ids)
    {
        return appUserMapper.deleteAppUserByIds(ids);
    }

    /**
     * 删除小程序用户信息
     *
     * @param id 小程序用户主键
     * @return 结果
     */
    @Override
    public int deleteAppUserById(Long id)
    {
        return appUserMapper.deleteAppUserById(id);
    }
}
